<?php

/*------------------------------------------------
*
*   Explay Engine Content Management System.
*   Copyright © 2008 Golovdinov Alexander.
*
*-------------------------------------------------
*
*   Official site: wwww.alex-home.spb.ru/explay.html
*   Contact e-mail: golovdinov@gmail.com
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
------------------------------------------------*/


if (!is_registered()) exit;
if (!isset($_POST['blog_id']) || !isset($_POST['type'])) exit;

$id = intval($_POST['blog_id']);


$check = $db->query ("SELECT cat_id FROM ".DB_PEREFIX."_art_category WHERE cat_id = '$id'");
if ($db->num_rows($check) != 1) exit;

Templater::load ('articles/blog_info.tpl', false);

if ($_POST['type'] == 'join' && !in_blog ($id)) {
	
	$db->query ("INSERT INTO ".DB_PEREFIX."_blogs_users VALUES ({$GLOBAL_USER['user_id']}, $id)");
	
	Templater::set_var ('id', $id);
	print Templater::display_part ('blog_info_exit');
}
elseif ($_POST['type'] == 'exit' && in_blog ($id)) {
	
	$db->query ("DELETE FROM ".DB_PEREFIX."_blogs_users WHERE blog = $id AND user = ".User::$properties['user_id']);
	
	Templater::set_var ('id', $id);
	print Templater::display_part ('blog_info_join');
	
}

exit;

function in_blog ($id) {
	global $db;
	
	$check = $db->num_rows ($db->query ("SELECT * FROM ".DB_PEREFIX."_blogs_users WHERE blog = $id AND user = ".User::$properties['user_id']));
	
	if ($check == 1)
		return true;
	else
		return false;
}
